#
# Copyright 2019 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

load("@bazel_skylib//:bzl_library.bzl", "bzl_library")
load("@rules_cc//cc:cc_binary.bzl", "cc_binary")
load("@rules_cc//cc:cc_library.bzl", "cc_library")
load("@rules_cc//cc:cc_test.bzl", "cc_test")
load(":builddefs.bzl", "gen_analyzer_test")

package(
    default_visibility = ["//zetasql/base:zetasql_implementation"],
)

filegroup(
    name = "analyzer_test_files",
    srcs = glob(["testdata/*.test"]),
)

cc_library(
    name = "constant_resolver_helper",
    srcs = ["constant_resolver_helper.cc"],
    hdrs = ["constant_resolver_helper.h"],
    deps = [
        "//zetasql/base:ret_check",
        "//zetasql/base:status",
        "//zetasql/public:analyzer_options",
        "//zetasql/public:sql_constant",
        "//zetasql/public:value",
        "//zetasql/resolved_ast",
        "@com_google_absl//absl/status",
        "@com_google_absl//absl/status:statusor",
    ],
)

cc_library(
    name = "container_hash_equals",
    hdrs = ["container_hash_equals.h"],
    deps = [
        "@com_google_absl//absl/hash",
        "@com_google_absl//absl/types:span",
    ],
)

cc_test(
    name = "container_hash_equals_test",
    srcs = ["container_hash_equals_test.cc"],
    deps = [
        ":container_hash_equals",
        "//zetasql/base/testing:zetasql_gtest_main",
        "//zetasql/public:id_string",
        "@com_google_absl//absl/container:flat_hash_map",
        "@com_google_absl//absl/strings",
        "@com_google_absl//absl/types:span",
    ],
)

cc_library(
    name = "function_signature_matcher",
    srcs = ["function_signature_matcher.cc"],
    hdrs = ["function_signature_matcher.h"],
    deps = [
        ":lambda_util",
        "//zetasql/base",
        "//zetasql/base:check",
        "//zetasql/base:map_util",
        "//zetasql/base:ret_check",
        "//zetasql/base:status",
        "//zetasql/base:strings",
        "//zetasql/common:thread_stack",
        "//zetasql/parser",
        "//zetasql/public:coercer",
        "//zetasql/public:function",
        "//zetasql/public:function_cc_proto",
        "//zetasql/public:id_string",
        "//zetasql/public:language_options",
        "//zetasql/public:options_cc_proto",
        "//zetasql/public:signature_match_result",
        "//zetasql/public:strings",
        "//zetasql/public:type",
        "//zetasql/public:value",
        "//zetasql/public/types",
        "//zetasql/resolved_ast",
        "@com_google_absl//absl/base:core_headers",
        "@com_google_absl//absl/status",
        "@com_google_absl//absl/status:statusor",
        "@com_google_absl//absl/strings",
        "@com_google_absl//absl/strings:str_format",
        "@com_google_absl//absl/types:span",
    ],
)

cc_library(
    name = "expr_matching_helpers",
    srcs = ["expr_matching_helpers.cc"],
    hdrs = ["expr_matching_helpers.h"],
    deps = [
        ":name_scope",
        "//zetasql/base:check",
        "//zetasql/base:ret_check",
        "//zetasql/base:status",
        "//zetasql/common:graph_element_utils",
        "//zetasql/public:function",
        "//zetasql/public:function_cc_proto",
        "//zetasql/public:id_string",
        "//zetasql/public:language_options",
        "//zetasql/public:options_cc_proto",
        "//zetasql/public:type",
        "//zetasql/public:value",
        "//zetasql/resolved_ast",
        "//zetasql/resolved_ast:resolved_node_kind_cc_proto",
        "@com_google_absl//absl/base:core_headers",
        "@com_google_absl//absl/container:flat_hash_set",
        "@com_google_absl//absl/hash",
        "@com_google_absl//absl/log",
        "@com_google_absl//absl/status:statusor",
        "@com_google_absl//absl/strings",
        "@com_google_absl//absl/types:span",
    ],
)

cc_library(
    name = "lambda_util",
    srcs = [
        "lambda_util.cc",
    ],
    hdrs = [
        "lambda_util.h",
    ],
    deps = [
        "//zetasql/base:logging",
        "//zetasql/base:status",
        "//zetasql/parser",
        "//zetasql/public:id_string",
        "@com_google_absl//absl/status",
        "@com_google_absl//absl/status:statusor",
        "@com_google_absl//absl/types:span",
    ],
)

cc_library(
    name = "name_scope",
    srcs = ["name_scope.cc"],
    hdrs = ["name_scope.h"],
    deps = [
        ":path_expression_span",
        "//zetasql/base",
        "//zetasql/base:map_util",
        "//zetasql/base:ret_check",
        "//zetasql/base:status",
        "//zetasql/base:strings",
        "//zetasql/common:reflection_cc_proto",
        "//zetasql/parser",
        "//zetasql/public:catalog",
        "//zetasql/public:id_string",
        "//zetasql/public:options_cc_proto",
        "//zetasql/public:parse_location",
        "//zetasql/public:strings",
        "//zetasql/public:type",
        "//zetasql/public/types:type_with_catalog_impl",
        "//zetasql/resolved_ast",
        "@com_google_absl//absl/base:core_headers",
        "@com_google_absl//absl/container:flat_hash_map",
        "@com_google_absl//absl/container:flat_hash_set",
        "@com_google_absl//absl/status",
        "@com_google_absl//absl/status:statusor",
        "@com_google_absl//absl/strings",
        "@com_google_absl//absl/types:span",
        "@com_google_googletest//:gtest",
    ],
)

cc_library(
    name = "resolver",
    srcs = [
        "analytic_function_resolver.cc",
        "column_cycle_detector.cc",
        "expr_resolver_helper.cc",
        "function_resolver.cc",
        "graph_expr_resolver_helper.cc",
        "graph_query_resolver.cc",
        "graph_stmt_resolver.cc",
        "query_resolver_helper.cc",
        "recursive_queries.cc",
        "recursive_queries.h",
        "resolver.cc",
        "resolver_alter_stmt.cc",
        "resolver_common_inl.h",
        "resolver_dml.cc",
        "resolver_expr.cc",
        "resolver_query.cc",
        "resolver_stmt.cc",
    ],
    hdrs = [
        "analytic_function_resolver.h",
        "column_cycle_detector.h",
        "expr_resolver_helper.h",
        "function_resolver.h",
        "graph_expr_resolver_helper.h",
        "graph_query_resolver.h",
        "graph_stmt_resolver.h",
        "named_argument_info.h",
        "query_resolver_helper.h",
        "resolver.h",
    ],
    deps = [
        ":annotation_propagator",
        ":constant_resolver_helper",
        ":container_hash_equals",
        ":expr_matching_helpers",
        ":filter_fields_path_validator",
        ":function_signature_matcher",
        ":input_argument_type_resolver_helper",
        ":lambda_util",
        ":name_scope",
        ":path_expression_span",
        ":resolver_util",
        ":set_operation_resolver_base",
        "//zetasql/analyzer/rewriters:rewrite_subpipeline",
        "//zetasql/base",
        "//zetasql/base:check",
        "//zetasql/base:general_trie",
        "//zetasql/base:map_util",
        "//zetasql/base:ret_check",
        "//zetasql/base:status",
        "//zetasql/base:stl_util",
        "//zetasql/base:strings",
        "//zetasql/base:varsetter",
        "//zetasql/common:constant_utils",
        "//zetasql/common:errors",
        "//zetasql/common:graph_element_utils",
        "//zetasql/common:internal_analyzer_options",
        "//zetasql/common:internal_analyzer_output_properties",
        "//zetasql/common:measure_utils",
        "//zetasql/common:reflection_cc_proto",
        "//zetasql/common:reflection_helper",
        "//zetasql/common:status_payload_utils",
        "//zetasql/common:thread_stack",
        "//zetasql/common:type_and_argument_kind_visitor",
        "//zetasql/common:warning_sink",
        "//zetasql/parser",
        "//zetasql/proto:internal_error_location_cc_proto",
        "//zetasql/proto:internal_fix_suggestion_cc_proto",
        "//zetasql/public:aggregation_threshold_utils",
        "//zetasql/public:analyzer_options",
        "//zetasql/public:analyzer_output_properties",
        "//zetasql/public:anon_function",
        "//zetasql/public:builtin_function_cc_proto",
        "//zetasql/public:catalog",
        "//zetasql/public:civil_time",
        "//zetasql/public:coercer",
        "//zetasql/public:constant",
        "//zetasql/public:constness_level_cc_proto",
        "//zetasql/public:cycle_detector",
        "//zetasql/public:deprecation_warning_cc_proto",
        "//zetasql/public:error_helpers",
        "//zetasql/public:error_location_cc_proto",
        "//zetasql/public:function",
        "//zetasql/public:function_cc_proto",
        "//zetasql/public:id_string",
        "//zetasql/public:interval_value",
        "//zetasql/public:json_value",
        "//zetasql/public:language_options",
        "//zetasql/public:numeric_value",
        "//zetasql/public:options_cc_proto",
        "//zetasql/public:parse_location",
        "//zetasql/public:parse_resume_location",
        "//zetasql/public:pico_time",
        "//zetasql/public:signature_match_result",
        "//zetasql/public:simple_catalog",
        "//zetasql/public:sql_function",
        "//zetasql/public:sql_tvf",
        "//zetasql/public:sql_view",
        "//zetasql/public:strings",
        "//zetasql/public:templated_sql_function",
        "//zetasql/public:templated_sql_tvf_no_resolver",
        "//zetasql/public:timestamp_picos_value",
        "//zetasql/public:type",
        "//zetasql/public:type_cc_proto",
        "//zetasql/public:value",
        "//zetasql/public:with_modifier_mode",
        "//zetasql/public/annotation:collation",
        "//zetasql/public/functions:array_zip_mode_cc_proto",
        "//zetasql/public/functions:convert_string",
        "//zetasql/public/functions:date_time_util",
        "//zetasql/public/functions:datetime_cc_proto",
        "//zetasql/public/functions:normalize_mode_cc_proto",
        "//zetasql/public/functions:range",
        "//zetasql/public/functions:regexp",
        "//zetasql/public/proto:type_annotation_cc_proto",
        "//zetasql/public/types",
        "//zetasql/resolved_ast",
        "//zetasql/resolved_ast:column_factory",
        "//zetasql/resolved_ast:comparator",
        "//zetasql/resolved_ast:make_node_vector",
        "//zetasql/resolved_ast:node_sources",
        "//zetasql/resolved_ast:resolved_ast_builder",
        "//zetasql/resolved_ast:resolved_ast_enums_cc_proto",
        "//zetasql/resolved_ast:resolved_ast_rewrite_visitor",
        "//zetasql/resolved_ast:resolved_node_kind_cc_proto",
        "//zetasql/resolved_ast:rewrite_utils",
        "//zetasql/resolved_ast:target_syntax",
        "//zetasql/scripting:parsed_script",
        "@com_google_absl//absl/algorithm:container",
        "@com_google_absl//absl/base",
        "@com_google_absl//absl/base:core_headers",
        "@com_google_absl//absl/base:nullability",
        "@com_google_absl//absl/cleanup",
        "@com_google_absl//absl/container:btree",
        "@com_google_absl//absl/container:flat_hash_map",
        "@com_google_absl//absl/container:flat_hash_set",
        "@com_google_absl//absl/container:node_hash_map",
        "@com_google_absl//absl/flags:flag",
        "@com_google_absl//absl/functional:any_invocable",
        "@com_google_absl//absl/log",
        "@com_google_absl//absl/memory",
        "@com_google_absl//absl/status",
        "@com_google_absl//absl/status:statusor",
        "@com_google_absl//absl/strings",
        "@com_google_absl//absl/strings:str_format",
        "@com_google_absl//absl/time",
        "@com_google_absl//absl/types:span",
        "@com_google_googletest//:gtest",
        "@com_google_protobuf//:protobuf",
        "@com_google_protobuf//:protobuf_lite",
    ],
)

cc_library(
    name = "set_operation_resolver_base",
    srcs = [
        "set_operation_resolver_base.cc",
    ],
    hdrs = [
        "set_operation_resolver_base.h",
    ],
    deps = [
        ":input_argument_type_resolver_helper",
        ":resolver_util",
        "//zetasql/base:ret_check",
        "//zetasql/base:status",
        "//zetasql/base:stl_util",
        "//zetasql/parser:parse_tree",
        "//zetasql/public:analyzer_options",
        "//zetasql/public:coercer",
        "//zetasql/public:function_headers",
        "//zetasql/public:id_string",
        "//zetasql/public/types",
        "//zetasql/resolved_ast",
        "//zetasql/resolved_ast:column_factory",
        "//zetasql/resolved_ast:resolved_node_kind_cc_proto",
        "@com_google_absl//absl/container:flat_hash_map",
        "@com_google_absl//absl/container:flat_hash_set",
        "@com_google_absl//absl/status",
        "@com_google_absl//absl/status:statusor",
        "@com_google_absl//absl/types:span",
    ],
)

cc_library(
    name = "input_argument_type_resolver_helper",
    srcs = [
        "input_argument_type_resolver_helper.cc",
    ],
    hdrs = [
        "input_argument_type_resolver_helper.h",
    ],
    deps = [
        ":constant_resolver_helper",
        "//zetasql/base",
        "//zetasql/base:check",
        "//zetasql/base:ret_check",
        "//zetasql/base:status",
        "//zetasql/parser",
        "//zetasql/public:analyzer_options",
        "//zetasql/public:function",
        "//zetasql/public:value",
        "//zetasql/public/types",
        "//zetasql/resolved_ast",
        "//zetasql/resolved_ast:resolved_node_kind_cc_proto",
        "@com_google_absl//absl/status",
        "@com_google_absl//absl/status:statusor",
        "@com_google_absl//absl/types:span",
    ],
)

# This library is a dependency of the public analyzer to register all of the
# internal ZetaSQL rewriters. The internal analyzer does not depend on
# it directly. This allows rewriters to depend on :substitute without creating
# a circular dependency. Since all binaries that use analyzer depend on
# public:analyzer, all the internal rewriters will be linked and registered
# in such binaries.
cc_library(
    name = "all_rewriters",
    srcs = ["all_rewriters.cc"],
    hdrs = ["all_rewriters.h"],
    deps = [
        "//zetasql/analyzer/rewriters:aggregation_threshold_rewriter",
        "//zetasql/analyzer/rewriters:anonymization_rewriter",
        "//zetasql/analyzer/rewriters:builtin_function_inliner",
        "//zetasql/analyzer/rewriters:flatten_rewriter",
        "//zetasql/analyzer/rewriters:generalized_query_stmt_rewriter",
        "//zetasql/analyzer/rewriters:grouping_set_rewriter",
        "//zetasql/analyzer/rewriters:insert_dml_values_rewriter",
        "//zetasql/analyzer/rewriters:is_first_is_last_function_rewriter",
        "//zetasql/analyzer/rewriters:like_any_all_rewriter",
        "//zetasql/analyzer/rewriters:map_function_rewriter",
        "//zetasql/analyzer/rewriters:match_recognize_function_rewriter",
        "//zetasql/analyzer/rewriters:measure_type_rewriter",
        "//zetasql/analyzer/rewriters:multiway_unnest_rewriter",
        "//zetasql/analyzer/rewriters:nulliferror_function_rewriter",
        "//zetasql/analyzer/rewriters:order_by_and_limit_in_aggregate_rewriter",
        "//zetasql/analyzer/rewriters:pipe_assert_rewriter",
        "//zetasql/analyzer/rewriters:pipe_describe_rewriter",
        "//zetasql/analyzer/rewriters:pipe_if_rewriter",
        "//zetasql/analyzer/rewriters:pivot_rewriter",
        "//zetasql/analyzer/rewriters:registration",
        "//zetasql/analyzer/rewriters:row_type_rewriter",
        "//zetasql/analyzer/rewriters:sql_function_inliner",
        "//zetasql/analyzer/rewriters:sql_view_inliner",
        "//zetasql/analyzer/rewriters:subpipeline_stmt_rewriter",
        "//zetasql/analyzer/rewriters:typeof_function_rewriter",
        "//zetasql/analyzer/rewriters:unpivot_rewriter",
        "//zetasql/analyzer/rewriters:update_constructor_rewriter",
        "//zetasql/analyzer/rewriters:variadic_function_signature_expander",
        "//zetasql/analyzer/rewriters:with_expr_rewriter",
        "//zetasql/public:options_cc_proto",
        "@com_google_absl//absl/base",
    ],
)

cc_library(
    name = "analyzer_output_mutator",
    srcs = ["analyzer_output_mutator.cc"],
    hdrs = ["analyzer_output_mutator.h"],
    deps = [
        "//zetasql/base:check",
        "//zetasql/base:ret_check",
        "//zetasql/common:status_payload_utils",
        "//zetasql/common:timer_util",
        "//zetasql/public:analyzer_output",
        "//zetasql/public:analyzer_output_properties",
        "//zetasql/public/proto:logging_cc_proto",
        "//zetasql/resolved_ast",
        "@com_google_absl//absl/flags:flag",
        "@com_google_absl//absl/status",
        "@com_google_absl//absl/status:statusor",
        "@com_google_absl//absl/strings",
    ],
)

cc_library(
    name = "analyzer_impl",
    srcs = ["analyzer_impl.cc"],
    hdrs = ["analyzer_impl.h"],
    deps = [
        ":analyzer_output_mutator",
        ":resolver",
        ":rewrite_resolved_ast",
        "//zetasql/base:logging",
        "//zetasql/base:status",
        "//zetasql/common:errors",
        "//zetasql/common:internal_analyzer_options",
        "//zetasql/common:timer_util",
        "//zetasql/parser",
        "//zetasql/public:analyzer_options",
        "//zetasql/public:analyzer_output",
        "//zetasql/public:catalog",
        "//zetasql/public:error_helpers",
        "//zetasql/public:language_options",
        "//zetasql/public:rewriter_interface",
        "//zetasql/public/types",
        "//zetasql/resolved_ast",
        "//zetasql/resolved_ast:validator",
        "@com_google_absl//absl/flags:flag",
        "@com_google_absl//absl/status",
        "@com_google_absl//absl/strings",
        "@com_google_absl//absl/types:span",
    ],
)

cc_library(
    name = "filter_fields_path_validator",
    srcs = ["filter_fields_path_validator.cc"],
    hdrs = ["filter_fields_path_validator.h"],
    deps = [
        "//zetasql/base:logging",
        "//zetasql/base:map_util",
        "//zetasql/base:ret_check",
        "//zetasql/base:status",
        "@com_google_absl//absl/container:flat_hash_map",
        "@com_google_absl//absl/status",
        "@com_google_absl//absl/strings",
        "@com_google_protobuf//:protobuf",
    ],
)

cc_test(
    name = "resolver_test",
    size = "small",
    srcs = ["resolver_test.cc"],
    deps = [
        ":name_scope",
        ":resolver",
        "//zetasql/base",
        "//zetasql/base:status",
        "//zetasql/base/testing:status_matchers",
        "//zetasql/base/testing:zetasql_gtest_main",
        "//zetasql/parser",
        "//zetasql/public:analyzer",
        "//zetasql/public:analyzer_options",
        "//zetasql/public:analyzer_output",
        "//zetasql/public:analyzer_output_properties",
        "//zetasql/public:catalog",
        "//zetasql/public:function",
        "//zetasql/public:function_cc_proto",
        "//zetasql/public:id_string",
        "//zetasql/public:language_options",
        "//zetasql/public:numeric_value",
        "//zetasql/public:options_cc_proto",
        "//zetasql/public:value",
        "//zetasql/public/types",
        "//zetasql/resolved_ast",
        "//zetasql/resolved_ast:resolved_node_kind_cc_proto",
        "//zetasql/testdata:error_catalog",
        "//zetasql/testdata:sample_catalog",
        "//zetasql/testdata:test_schema_cc_proto",
        "@com_google_absl//absl/status",
        "@com_google_absl//absl/status:statusor",
        "@com_google_absl//absl/strings",
        "@com_google_absl//absl/types:span",
    ],
)

cc_test(
    name = "analyzer_test",
    size = "small",
    timeout = "moderate",
    srcs = ["analyzer_test.cc"],
    data = [
        "//zetasql/testdata:external_extension.proto",
        "//zetasql/testdata:test_schema_proto",
    ],
    deps = [
        "//zetasql/base",
        "//zetasql/base:map_util",
        "//zetasql/base:ret_check",
        "//zetasql/base:status",
        "//zetasql/base/testing:status_matchers",
        "//zetasql/base/testing:zetasql_gtest_main",
        "//zetasql/common:errors",
        "//zetasql/common:status_payload_utils",
        "//zetasql/common/testing:testing_proto_util",
        "//zetasql/parser",
        "//zetasql/proto:options_cc_proto",
        "//zetasql/public:analyzer",
        "//zetasql/public:analyzer_options",
        "//zetasql/public:analyzer_output",
        "//zetasql/public:analyzer_output_properties",
        "//zetasql/public:builtin_function_options",
        "//zetasql/public:catalog",
        "//zetasql/public:error_helpers",
        "//zetasql/public:function",
        "//zetasql/public:function_cc_proto",
        "//zetasql/public:id_string",
        "//zetasql/public:language_options",
        "//zetasql/public:literal_remover",
        "//zetasql/public:options_cc_proto",
        "//zetasql/public:parse_resume_location",
        "//zetasql/public:rewriter_interface",
        "//zetasql/public:simple_catalog",
        "//zetasql/public:sql_formatter",
        "//zetasql/public:type",
        "//zetasql/public:type_cc_proto",
        "//zetasql/public:value",
        "//zetasql/public/types",
        "//zetasql/resolved_ast",
        "//zetasql/resolved_ast:resolved_node_kind_cc_proto",
        "//zetasql/resolved_ast:sql_builder",
        "//zetasql/testdata:sample_catalog",
        "//zetasql/testdata:test_schema_cc_proto",
        "@com_google_absl//absl/container:btree",
        "@com_google_absl//absl/container:flat_hash_map",
        "@com_google_absl//absl/container:flat_hash_set",
        "@com_google_absl//absl/container:node_hash_set",
        "@com_google_absl//absl/flags:flag",
        "@com_google_absl//absl/status",
        "@com_google_absl//absl/strings",
        "@com_google_absl//absl/time",
        "@com_google_absl//absl/types:span",
    ],
)

cc_test(
    name = "expr_resolver_helper_test",
    size = "small",
    srcs = ["expr_resolver_helper_test.cc"],
    deps = [
        ":resolver",
        "//zetasql/base/testing:status_matchers",
        "//zetasql/base/testing:zetasql_gtest_main",
        "//zetasql/parser:parse_tree",
        "//zetasql/parser:parse_tree_serializer",
        "//zetasql/public:analyzer",
        "//zetasql/public:analyzer_options",
        "//zetasql/public:analyzer_output",
        "//zetasql/public:id_string",
        "//zetasql/public:language_options",
        "//zetasql/public:templated_sql_tvf",
        "//zetasql/public/types",
        "//zetasql/resolved_ast",
        "//zetasql/testdata:sample_catalog",
        "@com_google_absl//absl/status",
        "@com_google_absl//absl/strings",
    ],
)

cc_test(
    name = "filter_fields_path_validator_test",
    size = "small",
    srcs = ["filter_fields_path_validator_test.cc"],
    deps = [
        ":filter_fields_path_validator",
        "//zetasql/base/testing:status_matchers",
        "//zetasql/base/testing:zetasql_gtest_main",
        "//zetasql/testdata:test_schema_cc_proto",
        "@com_google_absl//absl/status",
    ],
)

cc_library(
    name = "substitute",
    srcs = ["substitute.cc"],
    hdrs = ["substitute.h"],
    deps = [
        ":analyzer_impl",
        "//zetasql/base:logging",
        "//zetasql/base:map_util",
        "//zetasql/base:ret_check",
        "//zetasql/base:status",
        "//zetasql/common:errors",
        "//zetasql/common:internal_analyzer_options",
        "//zetasql/public:analyzer_options",
        "//zetasql/public:analyzer_output",
        "//zetasql/public:catalog",
        "//zetasql/public:function",
        "//zetasql/public:function_cc_proto",
        "//zetasql/public:multi_catalog",
        "//zetasql/public:options_cc_proto",
        "//zetasql/public:simple_catalog",
        "//zetasql/public/types",
        "//zetasql/resolved_ast",
        "//zetasql/resolved_ast:column_factory",
        "//zetasql/resolved_ast:rewrite_utils",
        "@com_google_absl//absl/cleanup",
        "@com_google_absl//absl/container:flat_hash_map",
        "@com_google_absl//absl/container:flat_hash_set",
        "@com_google_absl//absl/status",
        "@com_google_absl//absl/status:statusor",
        "@com_google_absl//absl/strings",
    ],
)

cc_test(
    name = "substitute_test",
    srcs = ["substitute_test.cc"],
    deps = [
        ":substitute",
        "//zetasql/base:check",
        "//zetasql/base:logging",
        "//zetasql/base/testing:status_matchers",
        "//zetasql/base/testing:zetasql_gtest_main",
        "//zetasql/public:analyzer",
        "//zetasql/public:analyzer_options",
        "//zetasql/public:analyzer_output",
        "//zetasql/public:builtin_function_options",
        "//zetasql/public:evaluator",
        "//zetasql/public:id_string",
        "//zetasql/public:language_options",
        "//zetasql/public:options_cc_proto",
        "//zetasql/public:simple_catalog",
        "//zetasql/public:value",
        "//zetasql/public/annotation:collation",
        "//zetasql/public/types",
        "//zetasql/resolved_ast",
        "//zetasql/testing:test_catalog",
        "@com_google_absl//absl/status",
        "@com_google_absl//absl/status:statusor",
        "@com_google_absl//absl/strings",
        "@com_google_absl//absl/types:span",
    ],
)

cc_test(
    name = "input_argument_type_resolver_helper_test",
    srcs = ["input_argument_type_resolver_helper_test.cc"],
    deps = [
        ":input_argument_type_resolver_helper",
        "//zetasql/base/testing:status_matchers",
        "//zetasql/base/testing:zetasql_gtest_main",
        "//zetasql/parser:parse_tree",
        "//zetasql/public:analyzer_options",
        "//zetasql/public:evaluator",
        "//zetasql/public:function",
        "//zetasql/public:prepared_expression_constant_evaluator",
        "//zetasql/public:simple_catalog_util",
        "//zetasql/public:sql_constant",
        "//zetasql/public:value",
        "//zetasql/public/types",
        "//zetasql/resolved_ast",
        "//zetasql/resolved_ast:resolved_ast_builder",
    ],
)

cc_library(
    name = "rewrite_resolved_ast",
    srcs = ["rewrite_resolved_ast.cc"],
    hdrs = ["rewrite_resolved_ast.h"],
    deps = [
        ":analyzer_output_mutator",
        ":builtin_only_catalog",
        "//zetasql/analyzer/rewriters:registration",
        "//zetasql/analyzer/rewriters:rewriter_relevance_checker",
        "//zetasql/analyzer/rewriters:templated_function_call_rewriter",
        "//zetasql/base:logging",
        "//zetasql/base:ret_check",
        "//zetasql/base:status",
        "//zetasql/common:errors",
        "//zetasql/common:internal_analyzer_options",
        "//zetasql/common:timer_util",
        "//zetasql/public:analyzer_options",
        "//zetasql/public:analyzer_output",
        "//zetasql/public:catalog",
        "//zetasql/public:language_options",
        "//zetasql/public:options_cc_proto",
        "//zetasql/public:rewriter_interface",
        "//zetasql/public/types",
        "//zetasql/resolved_ast",
        "//zetasql/resolved_ast:validator",
        "@com_google_absl//absl/algorithm:container",
        "@com_google_absl//absl/cleanup",
        "@com_google_absl//absl/container:btree",
        "@com_google_absl//absl/status",
        "@com_google_absl//absl/strings",
    ],
)

cc_test(
    name = "rewrite_resolved_ast_test",
    srcs = ["rewrite_resolved_ast_test.cc"],
    deps = [
        "//zetasql/base:check",
        "//zetasql/base/testing:status_matchers",
        "//zetasql/base/testing:zetasql_gtest_main",
        "//zetasql/public:analyzer",
        "//zetasql/public:analyzer_options",
        "//zetasql/public:analyzer_output",
        "//zetasql/public:builtin_function_options",
        "//zetasql/public:options_cc_proto",
        "//zetasql/public:rewriter_interface",
        "//zetasql/public:simple_catalog",
        "//zetasql/public:type",
        "//zetasql/public/types",
        "//zetasql/resolved_ast",
        "//zetasql/testdata:test_schema_cc_proto",
        "@com_google_absl//absl/flags:flag",
        "@com_google_absl//absl/status",
        "@com_google_absl//absl/strings:string_view",
    ],
)

cc_library(
    name = "analyzer_test_options",
    testonly = 1,
    srcs = ["analyzer_test_options.cc"],
    hdrs = ["analyzer_test_options.h"],
    deps = [
        "//zetasql/base:check",
        "//zetasql/base:ret_check",
        "//zetasql/base:status",
        "//zetasql/common:options_utils",
        "//zetasql/public:analyzer_options",
        "//zetasql/public:options_cc_proto",
        "//zetasql/public:type",
        "//zetasql/public/testing:test_case_options_util",
        "//zetasql/public/types",
        "//zetasql/testdata:test_schema_cc_proto",
        "@com_google_absl//absl/container:flat_hash_set",
        "@com_google_absl//absl/status:statusor",
        "@com_google_absl//absl/strings",
        "@com_google_absl//absl/types:span",
        "@com_google_file_based_test_driver//file_based_test_driver:test_case_options",
        "@com_google_protobuf//:protobuf",
    ],
)

cc_test(
    name = "analyzer_test_options_test",
    srcs = ["analyzer_test_options_test.cc"],
    deps = [
        ":analyzer_test_options",
        "//zetasql/base/testing:status_matchers",
        "//zetasql/base/testing:zetasql_gtest_main",
        "@com_google_absl//absl/status",
        "@com_google_file_based_test_driver//file_based_test_driver:test_case_options",
    ],
)

cc_library(
    name = "run_analyzer_test_lib",
    testonly = 1,
    srcs = ["run_analyzer_test.cc"],
    hdrs = ["run_analyzer_test.h"],
    deps = [
        ":analyzer_output_mutator",
        ":analyzer_test_options",
        "//zetasql/base",
        "//zetasql/base:enum_utils",
        "//zetasql/base:map_util",
        "//zetasql/base:ret_check",
        "//zetasql/base:status",
        "//zetasql/base/testing:status_matchers",
        "//zetasql/common:internal_analyzer_output_properties",
        "//zetasql/common:status_payload_utils",
        "//zetasql/common:unicode_utils",
        "//zetasql/examples/tpch/catalog",
        "//zetasql/parser",
        "//zetasql/public:analyzer",
        "//zetasql/public:analyzer_options",
        "//zetasql/public:analyzer_output",
        "//zetasql/public:analyzer_output_properties",
        "//zetasql/public:builtin_function_options",
        "//zetasql/public:catalog",
        "//zetasql/public:error_helpers",
        "//zetasql/public:evaluator",
        "//zetasql/public:function",
        "//zetasql/public:language_options",
        "//zetasql/public:literal_remover",
        "//zetasql/public:measure_expression",
        "//zetasql/public:multi_catalog",
        "//zetasql/public:options_cc_proto",
        "//zetasql/public:parse_helpers",
        "//zetasql/public:parse_resume_location",
        "//zetasql/public:prepared_expression_constant_evaluator",
        "//zetasql/public:simple_catalog",
        "//zetasql/public:simple_catalog_util",
        "//zetasql/public:sql_constant",
        "//zetasql/public:sql_formatter",
        "//zetasql/public:sql_function",
        "//zetasql/public:strings",
        "//zetasql/public:templated_sql_function",
        "//zetasql/public:templated_sql_tvf",
        "//zetasql/public:type",
        "//zetasql/public:value",
        "//zetasql/public/functions:date_time_util",
        "//zetasql/public/proto:logging_cc_proto",
        "//zetasql/public/testing:test_case_options_util",
        "//zetasql/public/types",
        "//zetasql/resolved_ast",
        "//zetasql/resolved_ast:comparator",
        "//zetasql/resolved_ast:resolved_ast_cc_proto",
        "//zetasql/resolved_ast:resolved_ast_enums_cc_proto",
        "//zetasql/resolved_ast:resolved_node_kind_cc_proto",
        "//zetasql/resolved_ast:sql_builder",
        "//zetasql/resolved_ast:target_syntax",
        "//zetasql/resolved_ast:validator",
        "//zetasql/testdata:bad_test_schema_cc_proto",
        "//zetasql/testdata:sample_annotation",
        "//zetasql/testdata:sample_catalog",
        "//zetasql/testdata:sample_system_variables",
        "//zetasql/testdata:special_catalog",
        "//zetasql/testdata:test_schema_cc_proto",
        "@com_google_absl//absl/algorithm:container",
        "@com_google_absl//absl/container:flat_hash_map",
        "@com_google_absl//absl/container:flat_hash_set",
        "@com_google_absl//absl/container:node_hash_map",
        "@com_google_absl//absl/flags:commandlineflag",
        "@com_google_absl//absl/flags:flag",
        "@com_google_absl//absl/flags:reflection",
        "@com_google_absl//absl/functional:bind_front",
        "@com_google_absl//absl/log",
        "@com_google_absl//absl/status",
        "@com_google_absl//absl/status:statusor",
        "@com_google_absl//absl/strings",
        "@com_google_absl//absl/strings:str_format",
        "@com_google_absl//absl/time",
        "@com_google_absl//absl/types:span",
        "@com_google_file_based_test_driver//file_based_test_driver",
        "@com_google_file_based_test_driver//file_based_test_driver:run_test_case_result",
        "@com_google_file_based_test_driver//file_based_test_driver:test_case_options",
        "@com_google_googletest//:gtest",
        "@com_google_protobuf//:protobuf",
        "@com_googlesource_code_re2//:re2",
    ],
    # Otherwise tests in this .cc file may get dropped by the linker when statically linking tests
    # which depend on this library, since these tests aren't referenced from main.
    alwayslink = True,
)

cc_library(
    name = "path_expression_span",
    srcs = ["path_expression_span.cc"],
    hdrs = ["path_expression_span.h"],
    deps = [
        "//zetasql/base:check",
        "//zetasql/base:ret_check",
        "//zetasql/parser:parse_tree",
        "//zetasql/public:id_string",
        "//zetasql/public:parse_location",
        "//zetasql/public:strings",
        "@com_google_absl//absl/base",
        "@com_google_absl//absl/status:statusor",
        "@com_google_absl//absl/strings",
    ],
)

cc_library(
    name = "annotation_propagator",
    srcs = [
        "annotation_propagator.cc",
    ],
    hdrs = [
        "annotation_propagator.h",
    ],
    deps = [
        "//zetasql/base:check",
        "//zetasql/base:ret_check",
        "//zetasql/base:status",
        "//zetasql/parser:parse_tree",
        "//zetasql/public:analyzer_options",
        "//zetasql/public:options_cc_proto",
        "//zetasql/public/annotation:collation",
        "//zetasql/public/types",
        "//zetasql/resolved_ast",
        "//zetasql/resolved_ast:resolved_node_kind_cc_proto",
        "@com_google_absl//absl/status",
    ],
)

cc_test(
    name = "annotation_propagator_test",
    srcs = [
        "annotation_propagator_test.cc",
    ],
    deps = [
        ":annotation_propagator",
        "//zetasql/base:check",
        "//zetasql/base:ret_check",
        "//zetasql/base:status",
        "//zetasql/base/testing:status_matchers",
        "//zetasql/base/testing:zetasql_gtest_main",
        "//zetasql/public:analyzer_options",
        "//zetasql/public:builtin_function_options",
        "//zetasql/public:catalog",
        "//zetasql/public:function_headers",
        "//zetasql/public:simple_catalog",
        "//zetasql/public/types",
        "//zetasql/resolved_ast",
        "//zetasql/resolved_ast:rewrite_utils",
        "//zetasql/resolved_ast:test_utils",
        "@com_google_absl//absl/status:statusor",
        "@com_google_absl//absl/strings",
    ],
)

cc_library(
    name = "builtin_only_catalog",
    srcs = ["builtin_only_catalog.cc"],
    hdrs = ["builtin_only_catalog.h"],
    deps = [
        "//zetasql/base:ret_check",
        "//zetasql/base:status",
        "//zetasql/public:catalog",
        "//zetasql/public:function",
        "//zetasql/public:function_headers",
        "//zetasql/public:strings",
        "//zetasql/public/types",
        "@com_google_absl//absl/log",
        "@com_google_absl//absl/memory",
        "@com_google_absl//absl/status",
        "@com_google_absl//absl/status:statusor",
        "@com_google_absl//absl/strings",
        "@com_google_absl//absl/types:span",
    ],
)

cc_test(
    name = "builtin_only_catalog_test",
    srcs = ["builtin_only_catalog_test.cc"],
    deps = [
        ":builtin_only_catalog",
        "//zetasql/base/testing:status_matchers",
        "//zetasql/base/testing:zetasql_gtest_main",
        "//zetasql/public:catalog",
        "//zetasql/public:constant",
        "//zetasql/public:function",
        "//zetasql/public:function_headers",
        "//zetasql/public:simple_catalog",
        "//zetasql/public:value",
        "//zetasql/public/types",
        "@com_google_absl//absl/status",
    ],
)

cc_test(
    name = "lookup_catalog_column_callback_test",
    srcs = ["lookup_catalog_column_callback_test.cc"],
    deps = [
        "//zetasql/base/testing:status_matchers",
        "//zetasql/base/testing:zetasql_gtest_main",
        "//zetasql/public:analyzer",
        "//zetasql/public:analyzer_options",
        "//zetasql/public:analyzer_output",
        "//zetasql/public:builtin_function_options",
        "//zetasql/public:catalog",
        "//zetasql/public:simple_catalog",
        "//zetasql/public/types",
        "//zetasql/resolved_ast",
        "@com_google_absl//absl/status",
        "@com_google_absl//absl/status:statusor",
        "@com_google_absl//absl/strings",
    ],
)

[gen_analyzer_test(
    filename = f.split("/")[-1],
) for f in glob(["testdata/*.test"])]

bzl_library(
    name = "builddefs_bzl",
    srcs = ["builddefs.bzl"],
    deps = [
        "@rules_cc//cc:core_rules",
    ],
)

cc_library(
    name = "resolver_util",
    srcs = ["resolver_util.cc"],
    hdrs = ["resolver_util.h"],
    deps = [
        "//zetasql/public:function_headers",
        "//zetasql/public/types:type",
        "@com_google_absl//absl/algorithm:container",
        "@com_google_absl//absl/types:span",
    ],
)
